﻿using Abp.Application.Services.Dto;
using Abp.Web.Models;
using Abp.Web.Mvc.Authorization;
using KDS.Mes.Authorization;
using KDS.Mes.Mes.Cartypes;
using KDS.Mes.Mes.Cartypes.Dtos;
using KDS.Mes.Web.Models.Cartypes;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace KDS.Mes.Web.Controllers
{
    public class CartypesController : MesControllerBase
    {
        private readonly ICartypeAppService _cartypeAppService;

        public CartypesController(ICartypeAppService cartypeAppService)
        {
            _cartypeAppService = cartypeAppService;
        }

        public ActionResult Index()
        {
            //var model = new GetCartypeInput { FilterText = Request.QueryString["filterText"] };
            return View();
        }

        [DontWrapResult]
        public JsonResult GetAllCartypes(int limit, int offset, string sortfiled, string sortway)
        {
            var sort = !string.IsNullOrEmpty(sortfiled) ? string.Format("{0} {1}", sortfiled, sortway) : "";
            var filter = new GetCartypeInput
            {
                SkipCount = offset,
                MaxResultCount = limit,
                Sorting = sort,
            };
            var pagedTasks = _cartypeAppService.GetPagedCartypes(filter);
            return Json(new { total = pagedTasks.TotalCount, rows = pagedTasks.Items }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 根据id获取进行编辑或者添加的用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AbpMvcAuthorize(AppPermissions.Pages_Bom_Cartypes_Create, AppPermissions.Pages_Bom_Cartypes_Edit)]
        public async Task<PartialViewResult> CreateOrEditCartypeModal(int? id)
        {
            var input = new NullableIdDto<int> { Id = id };

            var output = await _cartypeAppService.GetCartypeForEditAsync(input);

            var viewModel = new CreateOrEditCartypeModalViewModel(output);

            return PartialView("_CreateOrEditCartypeModal", viewModel);
        }
    }
}